//获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

function showData() {
//    overlayer("open");
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/get.do",
        data: {type: type, messageNo: id, instance: DepotName},
        dataType: "json",
        success: function(souData) {
            //souData = $.parseJSON(souData);
            var policy_desc = souData.Comment;
            $(".policy_desc").val(policy_desc);
            var data = souData.Content;
            var Web_Hit_Threshold = data.Web_Hit_Threshold;
            $("#Web_Hit_Threshold").val(Web_Hit_Threshold);
            var KW_Threholds = data.KW_Threholds;
            $("#KW_Threholds").val(KW_Threholds);
            var engines = data.SearchEngines.SearchEngine;
            var engine_textarea = "";
            for (var i = 0; i < engines.length; i++) {
                var engine = engines[i].SEName;
                engine_textarea += engine + "\n";
            }
            $("#engine-textarea").val(engine_textarea);
            var cookies = data.Cookie_Hosts.Cookie_Host;
            var cookie_textarea = "";
            for (var j = 0; j < cookies.length; j++) {
                var cookie = cookies[j].name + "," + cookies[j].key;
                cookie_textarea += cookie + "\n";
            }
            $("#cookie-textarea").val(cookie_textarea);
        }
    });


}

//获取表单数据
function generateParameter() {
    var obj = {};
    obj.Web_Hit_Threshold = $("#Web_Hit_Threshold").val();
    obj.KW_Threholds = $("#KW_Threholds").val();

    obj.SearchEngines = {};
    obj.SearchEngines.SearchEngine = [];
    var engines = $("#engine-textarea").val().trim().split("\n");
    for (var i = 0; i < engines.length; i++) {
        var tmp = {};
        tmp.SEName = engines[i];
        obj.SearchEngines.SearchEngine.push(tmp);
    }
    obj.Cookie_Hosts = {};
    obj.Cookie_Hosts.Cookie_Host = [];
    var cookies = $("#cookie-textarea").val().trim().split("\n");
    var flag = false;
    var o = {};
    for (var j = 0; j < cookies.length; j++) {
        if (cookies[j].indexOf(",") < 0) { // 有未打逗号的值
            if (flag == false) {
                if (confirm("检测到数据中含有未打“,”的值，是否舍弃？")) {
                    // 舍弃数据
                    flag = true;
                    continue;
                } else {
                    return; // 不舍弃数据，重新编辑
                }
            } else {
                continue; // 舍弃数据
            }
        }
        var tmp = {};

        var cookie = cookies[j].split(",");
        // if (o[cookie[1]]) {
        //     // key已经存在
        //     alert("输入了重复的key值！");
        //     return;
        // } else {
        //     // key不存在
        //     o[cookie[1]] = cookie[0];
        //     tmp.name = cookie[0];
        //     tmp.key = cookie[1];
        //     obj.Cookie_Hosts.Cookie_Host.push(tmp);
        // }
        if(o[cookies[j]]){
            console.log("输入了重复的值: "+cookies[j]+" ！");
        }else{
            o[cookies[j]] = true;
            tmp.name = cookie[0];
            tmp.key = cookie[1];
            obj.Cookie_Hosts.Cookie_Host.push(tmp);
        }

    }
    obj.MessageSerialNo = "111"; //写死
    return obj;
}

//添加数据
function save() {
    var form = $(".form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var comment = $(".policy_desc").val();
    var policy = generateParameter();
    if (!policy) {
        return;
    }
    policy = JSON.stringify(policy);
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/create.do",
        data: {comment: comment, policy: policy, type: type, instance: DepotName},
        dataType: "json",
        success: function(data) {
            if (data.result == "true") {
                alert("添加成功！");
                previous();
            } else {
                alert("添加失败!");
            }
        }
    });
}

//编辑数据
function edit() {
    var form = $(".form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var comment = $(".policy_desc").val();
    var policy = generateParameter();
    if (!policy) {
        return;
    }
    policy = JSON.stringify(policy);
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/update.do",
        data: {messageNo: id, comment: comment, policy: policy, type: type, instance: DepotName},
        dataType: "json",
        success: function(data) {
            if (data.result == "true") {
                alert("编辑成功！");
                previous();
            } else {
                alert("编辑失败!")
            }
        }
    });
}

//返回上一级
function previous() {
    window.location = "../policyList.html?type=" + type + "&id=" + DepotID + "&name=" + base64.encode(DepotName) + "";
}

//$(function(){
//    setInterval(function () {
//        parent.$("#page-content").attr("height", $("html").height());
//    }, 500);
//});
/*
 * 查看旧版本的策略内容
 */
function show_oldPolicy(array, number) {
    var souData = array[number];
    $(".header").text("当前策略在" + souData.CreateTime + "创建,ID为" + souData.MessageNo + ",版本号为" + souData.MessageSerialNo + ",属于" + souData.Instance + "策略库");
    
    var policy_desc = souData.Comment;
    $(".policy_desc").val(policy_desc);
    var data = souData.Content;
    var Web_Hit_Threshold = data.Web_Hit_Threshold;
    $("#Web_Hit_Threshold").val(Web_Hit_Threshold);
    var KW_Threholds = data.KW_Threholds;
    $("#KW_Threholds").val(KW_Threholds);
    var engines = data.SearchEngines.SearchEngine;
    var engine_textarea = "";
    for (var i = 0; i < engines.length; i++) {
        var engine = engines[i].SEName;
        engine_textarea += engine + "\n";
    }
    $("#engine-textarea").val(engine_textarea);
    var cookies = data.Cookie_Hosts.Cookie_Host;
    var cookie_textarea = "";
    for (var j = 0; j < cookies.length; j++) {
        var cookie = cookies[j].name + "," + cookies[j].key;
        cookie_textarea += cookie + "\n";
    }
    $("#cookie-textarea").val(cookie_textarea);
}

/*
 *获取所有版本的策略内容 
 */
function getAllPolicy() {
    $.ajax({
        type: "GET",
        async: false,
        url: "../../action/policy/history_policy_info.do",
        data: {type: type, instance: getQueryString("DepotName"), messageNo: id},
        dataType: "json",
        success: function(data) {
            policyArray = data;
            policyAmount = data.length
        }
    });
}